#####################################################################
# File:         Folder.py
# Product:      KnowledgeEngine
#
# Copyright:    Copyright (c) 2002, Faulkner Ltd.
# Author:       Brian Oliver
#
# Description:  This file defines the Folder class, a simple
#               Element that may container other Elements.
#
# See Also:     ElementContainer, Element
#####################################################################

#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.KnowledgeEngineCore import *
from Products.KnowledgeEngine.ElementContainer import ElementContainer, ElementContainerInfo

#####################################################################
## Zope Library Imports
from Globals import DTMLFile, MessageDialog
from ImageFile import ImageFile
from OFS import XMLExportImport

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## FolderInfo Class Definition
class FolderInfo(ElementContainerInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return "Folder"

    #---------------------------------------------------------------#

    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "Folder"
        
    #---------------------------------------------------------------#

    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "fold"
        
    #---------------------------------------------------------------#
    
    def getClassIcon(self):
        """
            Return the appropriate icon file for the class.
        """
        
        return ImageFile('icon.gif', globals())

    #---------------------------------------------------------------#

    def isChildPermitted(self, childClassInfo, childInstance = None):
        """
            Returns if the specified child is permitted as a 
            child of the class' instances.
            
            NOTE: This is from this class' instance perspective.
            
            For elements containers, this always overrides what the
            return value from childClassInfo.isPermittedInContainer(...)
        """
        
        #Folders may contain that of ElementContainers and Models and Libraries
        return ElementContainerInfo.isChildPermitted(self, childClassInfo, childInstance) or childClassInfo.isInstanceOf('Model') or childClassInfo.isInstanceOf('Library')
   
    #---------------------------------------------------------------#

    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """

        #initialise the super-class properties for reflection
        ElementContainerInfo.onConfigureProperties(self)

    #---------------------------------------------------------------#

#####################################################################
## Folder Class Definition
class Folder(ElementContainer):
    """
        A class to represent a simple folder that may contain Elements
    """

    #---------------------------------------------------------------#

    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = FolderInfo()

    #---------------------------------------------------------------#

    #this container is like a Zope folder!
    isPrincipiaFolderish = true
    
    #---------------------------------------------------------------#

    def __init__(self, id, identity):
        "Constructor for the Folder"        

        ElementContainer.__init__(self, id, identity)
                
        #perform onAfterInstanciation processing
        self.onAfterInstanciation()

    #---------------------------------------------------------------#

    def onAfterInstanciation(self):
        """
            Handle Knowledge Engine specific actions after this object 
            has been loaded into memory.  
            
            This is useful for performing backwards compatibility
        """        
        
        #perform default processing
        ElementContainer.onAfterInstanciation(self)
                
    #---------------------------------------------------------------#

    def isDisplayedBeforeComponents(self):
        """
            Returns if the Element is to be displayed before
            components in a model. Alternatively the Element is
            displayed after the components in a model.
            (If the element is in a model)
        """
        
        #Folders are always displayed after components when in a Model
        return false
        
    #---------------------------------------------------------------#

    def isPreviewPermitted(self):
        """
            Return if the Element is allowed to be previewed.
            
            By default all elements can't be previewed, some however
            should can be previewed. eg: HTMLHeaderFooters
        """
        
        return false

    #---------------------------------------------------------------#

    def getZopeMetaTypes(self):
        """
            ABSTRACT: Returns specialized Zope all_meta_types tuple
            for the implementing class.
            
            NOTE: override this method to provide specialized
            all_meta_types tuple the implementing class
        
            NOTE: You should probably include the super class result 
            of this method when overriding.
        """
        
        #the default is no specialized Meta Types.
        return (
            {'name': 'DTML Document','action': 'manage_addProduct/OFSP/documentAdd'},
            {'name': 'DTML Method','action': 'manage_addProduct/OFSP/methodAdd'},
            {'name': 'File','action': 'manage_addProduct/OFSP/fileAdd'},
            {'name': 'Image','action': 'manage_addProduct/OFSP/imageAdd'},
        )
         
    #---------------------------------------------------------------#

    customImporters = {
        XMLExportImport.magic: XMLExportImport.importXML,
        }

    # overrides Zope's default importExport form when in a KnowledgeEngine Environment
    # comment out the following line to revert to default importExport form
    manage_importExportForm = DTMLFile('dtml/manage_importExportForm', globals())

    def manage_importModel(self, REQUEST, set_owner=1):
        "import a model from the local drive"
        connection = self._p_jar
        
        while connection is None:
            connection = self._p_jar
        ob = connection.importFile(REQUEST.file, customImporters = self.customImporters)

        if REQUEST: self._verifyObjectPaste(ob, validate_src=0)
        id = ob.id
        if hasattr(id, 'im_func'):
            id = id()
        self._setObject(id, ob, set_owner=set_owner)
        ob = self._getOb(id)
        ob.manage_changeOwnershipType(explicit=0)
        
        # possibly add a check for meta_type to allow only upload of models ?

        if REQUEST is not None:
            return MessageDialog(
                title='Object Imported',
                message='<em>%s</em> was sucessfully imported' % id,
                action='manage_workspace'
                )

#####################################################################
## Class Initialisation

# Register Class Information with the Knowledge Engine
registerGenericObjectClass(Folder)

#####################################################################
